<html>
<head><meta charset="utf-8"><title>Panic VS abort through FFI · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html">Panic VS abort through FFI</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="207240831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207240831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207240831">(Aug 18 2020 at 09:45)</a>:</h4>
<p>Hi,<br>
I have a no-std crate with FFI bindings to C,<br>
There's an error callback I've configured that is called from C in case of a problem,<br>
This function currently panics but unwinding into C is UB, so I want to replace that with abort.<br>
But then I lose the printing that panic does, is there a best practice here? I can't print before aborting because it is no-std.<br>
I wish there was a panic without unwind option. <br>
Will <a class="stream" data-stream-id="210922" href="/#narrow/stream/210922-project-ffi-unwind">#project-ffi-unwind</a> help here?</p>



<a name="207241538"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207241538" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207241538">(Aug 18 2020 at 09:55)</a>:</h4>
<p>There are a few options.</p>



<a name="207241561"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207241561" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207241561">(Aug 18 2020 at 09:55)</a>:</h4>
<p><span class="user-mention" data-user-id="249222">@Elichai Turkel</span> ffi-unwind will solve the problem by letting you define the bindings as <code>extern "C-unwind"</code>, which will allow unwinding without UB.</p>



<a name="207241635"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207241635" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207241635">(Aug 18 2020 at 09:56)</a>:</h4>
<p><span class="user-mention silent" data-user-id="239881">Josh Triplett</span> <a href="#narrow/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI/near/207241561">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="249222">Elichai Turkel</span> ffi-unwind will solve the problem by letting you define the bindings as <code>extern "C-unwind"</code>, which will allow unwinding without UB.</p>
</blockquote>
<p>How can it allow unwinding through <em>C</em> (not C++) safely? (it's ok to say "it's complicated go read the RFC" :) )</p>



<a name="207241647"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207241647" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207241647">(Aug 18 2020 at 09:57)</a>:</h4>
<p>It's fairly simple, though you should still read the RFC. ;)</p>



<a name="207241662"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207241662" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207241662">(Aug 18 2020 at 09:57)</a>:</h4>
<p>C stack frames can be unwound.</p>



<a name="207241738"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207241738" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207241738">(Aug 18 2020 at 09:58)</a>:</h4>
<p>What are the other options until <code>"C-unwind"</code> will be a thing? <br>
(FYI, this is the code(pretty simple) <a href="https://github.com/rust-bitcoin/rust-secp256k1/blob/master/secp256k1-sys/src/lib.rs#L364">https://github.com/rust-bitcoin/rust-secp256k1/blob/master/secp256k1-sys/src/lib.rs#L364</a> )</p>



<a name="207241775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207241775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207241775">(Aug 18 2020 at 09:58)</a>:</h4>
<p>Can you elaborate on "I can't print before aborting"?</p>



<a name="207241788"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207241788" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207241788">(Aug 18 2020 at 09:59)</a>:</h4>
<p>Normally, <code>panic!</code> will print its message, even if panic="abort".</p>



<a name="207241801"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207241801" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207241801">(Aug 18 2020 at 09:59)</a>:</h4>
<p>It just doesn't unwind if panic="abort".</p>



<a name="207241872"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207241872" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207241872">(Aug 18 2020 at 10:00)</a>:</h4>
<p>As long as you don't expect to <code>catch_unwind</code> anywhere, you can just set <code>panic="abort"</code> and you won't unwind.</p>



<a name="207241914"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207241914" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207241914">(Aug 18 2020 at 10:00)</a>:</h4>
<p>It's a library, I shouldn't set <code>panic=abort</code> I thought I should replace the panic call with a <code>libc::abort()</code> or similar (too bad there's no stable <code>abort</code> in core)</p>



<a name="207241931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207241931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207241931">(Aug 18 2020 at 10:01)</a>:</h4>
<p>Ah, I see.</p>



<a name="207242070"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207242070" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207242070">(Aug 18 2020 at 10:03)</a>:</h4>
<p>You can't set <code>panic="abort"</code> yourself, but you <em>can</em> document that users should use <code>panic="abort"</code>, and you can use <code>catch_unwind</code> to stop panics from exiting your callback and call abort in that case.</p>



<a name="207242098"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207242098" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207242098">(Aug 18 2020 at 10:03)</a>:</h4>
<p>Or you could call the same messaging machinery that <code>panic!</code> would call, then abort, but that might be harder. :)</p>



<a name="207242178"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207242178" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207242178">(Aug 18 2020 at 10:04)</a>:</h4>
<p>(The latter would also not call the panic hook, and you probably want to call the panic hook.)</p>



<a name="207242244"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207242244" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207242244">(Aug 18 2020 at 10:06)</a>:</h4>
<p>Also, we're looking at adding a <code>cfg</code> mechanism for panic strategy: <a href="https://github.com/rust-lang/rust/pull/74754">https://github.com/rust-lang/rust/pull/74754</a></p>



<a name="207242319"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207242319" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207242319">(Aug 18 2020 at 10:06)</a>:</h4>
<p>That would let you write something like <code>cfg(not(panic = "abort")) compile_error!(...)</code>, or perhaps <code>cfg(all(feature = "no_std", not(panic = "abort"))) compile_error!(...)</code></p>



<a name="207242594"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207242594" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207242594">(Aug 18 2020 at 10:10)</a>:</h4>
<p>ha, that's pretty cool, I still think an <code>abort</code> macro can be cool, but what should I do that is already stable now? :)<br>
Just abort and not print anything? that would suck for users if it ever happen</p>



<a name="207242724"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207242724" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207242724">(Aug 18 2020 at 10:13)</a>:</h4>
<p>(FYI <code>catch_unwind</code> is in <code>std</code> :/)</p>



<a name="207245503"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207245503" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207245503">(Aug 18 2020 at 10:51)</a>:</h4>
<p>How is <code>panic!</code> currently printing when you're built with <code>no_std</code>?</p>



<a name="207248249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207248249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207248249">(Aug 18 2020 at 11:29)</a>:</h4>
<p>that's up to the end user and his panic impl, ie I have a regular std binary using it and it prints like any other panic</p>



<a name="207931609"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207931609" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207931609">(Aug 25 2020 at 07:23)</a>:</h4>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span> I just found this:</p>
<blockquote>
<p>Functions with an ABI that differs from "Rust" do not support unwinding in the exact same way that Rust does. Therefore, unwinding past the end of functions with such ABIs causes the process to abort.</p>
</blockquote>
<p><a href="https://github.com/rust-lang/reference/blob/master/src/items/functions.md#extern-function-qualifier">https://github.com/rust-lang/reference/blob/master/src/items/functions.md#extern-function-qualifier</a></p>
<p>does this mean I'm in the clear and there's no UB here?</p>
<p>CC <span class="user-mention" data-user-id="116009">@nikomatsakis</span> <a href="https://github.com/rust-lang/rust/pull/46833">https://github.com/rust-lang/rust/pull/46833</a></p>



<a name="207934567"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207934567" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207934567">(Aug 25 2020 at 08:05)</a>:</h4>
<p>We tried several times to make unwind from/into C abort, but in all cases that was reverted because it broke existing libraries. That piece of text is likely a leftover from one of those times.</p>



<a name="207938099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207938099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207938099">(Aug 25 2020 at 08:53)</a>:</h4>
<p>Somebody who knows what the current behavior is, please send a PR or file an issue explaining the current situation please.</p>



<a name="207958983"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207958983" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207958983">(Aug 25 2020 at 13:00)</a>:</h4>
<p>I believe an RFC was recently accepted relating to this</p>



<a name="207959133"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/207959133" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#207959133">(Aug 25 2020 at 13:01)</a>:</h4>
<p><a href="https://rust-lang.github.io/rfcs/2945-c-unwind-abi.html">https://rust-lang.github.io/rfcs/2945-c-unwind-abi.html</a></p>



<a name="208326920"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Panic%20VS%20abort%20through%20FFI/near/208326920" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Panic.20VS.20abort.20through.20FFI.html#208326920">(Aug 28 2020 at 10:36)</a>:</h4>
<p>The RFC also says it doesn't abort and it is UB. which contradicts the reference.</p>
<p>Posted a question on this thread: <a href="https://github.com/rust-lang/rust/issues/52652#issuecomment-682454449">https://github.com/rust-lang/rust/issues/52652#issuecomment-682454449</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>